package control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.Comment;
import dbconn.DBConnect;

/********
 * 留言管理类
 * 添加留言
 * 删除留言
 * 获取所有留言
 * @author Administrator
 *
 */
public class CommentControl {
	public static boolean Comment(Comment r) {
		DBConnect dbConnect = new DBConnect();
		String sql = "insert into comment(name,email,content,info,addtime,tel) values(?,?,?,?,?,?)";
		dbConnect.prepareStatement(sql);
		try {
			dbConnect.setString(1, r.getName());
			dbConnect.setString(2, r.getEmail());
			dbConnect.setString(3, r.getContent());
			dbConnect.setString(4, r.getInfo());
			dbConnect.setString(5, r.getAddtime());
			dbConnect.setString(6, r.getTel());
			dbConnect.executeUpdate();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}finally{
			dbConnect.close();
		}
	}
	
	public static boolean deleteComment(int id){
		DBConnect dbc = new DBConnect();
		String sql = "delete from comment where id=?";
		dbc.prepareStatement(sql);
		try{
			dbc.setInt(1, id);
			dbc.executeUpdate();
			return true;
		}catch(SQLException e){
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
	}
	
	public static List<Comment> getAllComment(int targetId){
		DBConnect dbConnect = new DBConnect();
		List<Comment> list = new ArrayList<Comment>();
		String sql = "select * from comment where targetid= '"+targetId+"'";
		ResultSet rSet = null; 
		dbConnect.prepareStatement(sql);
		try {
			rSet = dbConnect.executeQuery();
			while(rSet.next()){
				Comment c = new Comment();
				c.setId(rSet.getInt("id"));
				c.setContent(rSet.getString("content"));
				c.setAddtime(rSet.getString("addtime"));
				c.setEmail(rSet.getString("email"));
				c.setInfo(rSet.getString("info"));
				c.setName(rSet.getString("name"));
				c.setTel(rSet.getString("tel"));
				list.add(c);
			}
			return list;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}finally{
			dbConnect.close();
		}
	}
	

}
